/**
 * Created by bys on 2015/12/11.
 */

/*dialog弹窗初始化*/
function setDioalog(dialogId) {
    dialogId.dialog({
        dialogClass: "my-dialog",
        autoOpen: false,
        modal: true
    });
}

/*json配置*/
function setJson(type,pageNum){
    var jsonStr = {};
    if(parseInt(type) == 1){//机构页面翻页的json
        jsonStr.field = $.attr("flg");
        jsonStr['flg'] = 1;
        jsonStr.field = $.attr("name");
        jsonStr['name'] = "";
        jsonStr.field = $.attr("orgId");
        jsonStr['orgId'] = "";
        jsonStr.field = $.attr("orgId");
        jsonStr['orgId'] = "";
        jsonStr.field = $.attr("page");
        jsonStr['page'] = parseInt(pageNum);
    }else if(parseInt(type) == 3){//号段页面选择机构时翻页的json
        jsonStr.field = $.attr("flg");
        jsonStr['flg'] = 2;
        jsonStr.field = $.attr("name");
        jsonStr['name'] = "";
        jsonStr.field = $.attr("orgId");
        jsonStr['orgId'] = "";
        jsonStr.field = $.attr("orgId");
        jsonStr['orgId'] = "";
        jsonStr.field = $.attr("page");
        jsonStr['page'] = parseInt(pageNum);
    }else if(parseInt(type) == 2){//号段页面翻页的json
        jsonStr.field = $.attr("flg");
        jsonStr['flg'] = 1;
        jsonStr.field = $.attr("orgName");
        jsonStr['orgName'] = "";
        jsonStr.field = $.attr("orgCode");
        jsonStr['orgCode'] = "";
        jsonStr.field = $.attr("name");
        jsonStr['name'] = "";
        jsonStr.field = $.attr("page");
        jsonStr['page'] = parseInt(pageNum);
    }
    else if(parseInt(type) == 4)  // 资料&说明页面翻页的json
    {
        jsonStr.field = $.attr("page");
        jsonStr['page'] = parseInt(pageNum);
        jsonStr.field = $.attr("isSearch");
        jsonStr['isSearch'] = true;
    }else if(parseInt(type) == 5)  // 资料&说明页面翻页的用户登录
    {
        jsonStr.field = $.attr("page");
        jsonStr['page'] = parseInt(pageNum);
    }
    return jsonStr;
}
/*翻页功能star*/
/*上一页*/
function prevPage(curPageId, url,type,reLoadId,currentPageNum,countPageNum) {
    var toPageNum = curPageId.val();
    if(parseInt(type) == 3){
        doListUrl = toListUrl;
    }
    if (parseInt(toPageNum) == parseInt(currentPageNum)) {
        switch (parseInt(toPageNum)) {
            case 1:
                alert("当前页已经是第一页了！");
                break;
            default:
                if (parseInt(toPageNum) > 1) {
                    parseInt(toPageNum--);
                    var data = setJson(type,toPageNum);
                    doList(data, url+doListUrl, reLoadId);
                }
                break;
        }
    } else if (parseInt(toPageNum) > parseInt(currentPageNum)) {
        alert("请输入小于当前页码（" + currentPageNum + "）的数字！");
    } else {
        //alert("跳转到输入:" + toPageNum + "的页面");
        var data = setJson(type,toPageNum);
        doList(data, url+doListUrl, reLoadId);
    }
}
/*下一页*/
function nextPage(curPageId, url,type,reLoadId,currentPageNum,countPageNum) {
    var toPageNum = curPageId.val();
    if(parseInt(type) == 3){
        doListUrl = toListUrl;
    }
    if(parseInt(countPageNum) != 0){
        if (parseInt(toPageNum) == parseInt(currentPageNum)) {
            if (parseInt(toPageNum) == parseInt(countPageNum)) {
                alert("当前页已经是最后一页了！");
            } else if (parseInt(toPageNum) < parseInt(countPageNum)) {
                parseInt(toPageNum++);
                var data = setJson(type,toPageNum);
                doList(data, url+doListUrl, reLoadId);
            }
        } else if (parseInt(toPageNum) < parseInt(currentPageNum)) {
            alert("请输入大于当前页码（" + currentPageNum + "）的数字！");
        } else if (parseInt(toPageNum) > parseInt(countPageNum)) {
            alert("请输入小于总页码（" + countPageNum + "）的数字！");
        } else {
            //alert("跳转到输入:" + toPageNum + "的页面");
            var data = setJson(type,toPageNum);
            doList(data, url+doListUrl, reLoadId);
        }
    }else {
        alert("总页码数为:" + countPageNum + "请为表中添加新的数据！");
    }

}
/*翻页功能end*/

/*
 * *功能:跳转请求
 * *data:传递数据
 * url：请求链接
 * ObjId:Object控件的id
 * */
function doList(data, url,ObjId) {
    $.ajax({
        cache: true,
        type: "POST",
        url: url,
        data: data,
        async: false,
        error: function (request) {
            alert("Connection error");
        },
        success: function (result) {
            ObjId.html(result);
        }
    });
}

/**
 * 保存
 *
 * @param url
 */
function store(url, dlg, form)
{
    $.ajax({
        cache: true,
        type: "POST",
        url: url,
        data: form.serialize(),
        async: false,
        error: function (request) {
            alert("网络连接失败！");
        },
        success: function (result) {
            var data = $.parseJSON(result);
            if (data.ok)
            {
                dlg.dialog("close");
                window.location.reload(false);
            }
            else
            {
                $("#info_msg").text(data.msg);
                $("#info_dialog").dialog({
                    dialogClass: "my-dialog",
                    autoOpen: true,
                    modal: true,
                    title: '提示信息',
                    buttons: {
                        "确定": function () {
                            $(this).dialog("close");
                        }
                    }
                });
            }
        }
    });
}

/*数据校验start*/
/*validate校验start*/
function updateTips(t) {
    tips
        .text(t)
        .addClass("ui-state-highlight");
    //setTimeout(function () {
    //    tips.removeClass("ui-state-highlight", 1500);
    //}, 500);
}

function checkLength(o, n, min, max) {
    if (o.val().length > max || o.val().length < min) {
        o.addClass("ui-state-error");
        updateTips("请输入 " + n + " 其长度必须在 " +
            min + " 和 " + max + "之间。");
        return false;
    } else {
        return true;
    }
}

function checkRegexp(o, regexp, n) {
    if (!( regexp.test(o.val()) )) {
        o.addClass("ui-state-error");
        updateTips(n);
        return false;
    } else {
        return true;
    }
}
/*数据校验end*/